package com.cai.oamanager.controller;

import com.cai.oamanager.entity.Depart;
import com.cai.oamanager.entity.PageUtil;
import com.cai.oamanager.service.DepartService;
import lombok.Builder;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/18 17 : 44
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("depart")
public class DepartController {

    @Autowired
    private DepartService departService;

    @RequestMapping("depart.html")
    public String depart(Model model,@RequestParam(defaultValue = "1") Integer currentPage){
        PageUtil pageUtil = new PageUtil();
        Integer countDepart = departService.getCountDepart();
        pageUtil.setSize(5);
        pageUtil.setTotal(countDepart);
        pageUtil.setCurrentPage(currentPage);
        pageUtil.startPage();
        pageUtil.totalNumPage();
        List<Depart> departList = departService.getAllDepart(pageUtil.getStart(),pageUtil.getSize());

        model.addAttribute("departList",departList);
        model.addAttribute("pageUtil",pageUtil);
        return "depart";
    }

    @RequestMapping("delete/{id}")
    public String deleteDepart(@PathVariable("id") Integer id){
        departService.deleteDepart(id);
        return "redirect:/depart/depart.html";
    }

    @RequestMapping("toEdit")
    public String toEdit(Model model,@RequestParam(value = "id") Integer id){
        Depart depart = departService.getDepart(id);
        model.addAttribute("depart",depart);
        return "departEdit";
    }

    @RequestMapping("updateDepart")
    public String updateDepart(Depart depart){
        departService.updateDepart(depart);
        return "redirect:/depart/depart.html";
    }
}
